package data;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import domain.Subject;

public class JDBCSubjectDAO implements ISubjectDAO{

	@Override
	public List<Subject> selectAllSubjects() {
		 Connection conn = ConnectionManager.getInstance().checkOut();

	        PreparedStatement stmt = null;
	        List<Subject> searchResults = new LinkedList<Subject>();
	        ResultSet result = null;

	        try {

	            String sql = "SELECT * FROM Subjects";
	            stmt = conn.prepareStatement(sql);
	            stmt.executeQuery();
	            result = stmt.executeQuery();

	            while (result.next()) {
	                Subject temp = new Subject();
	                temp.setId(result.getInt("id"));
	                temp.setName(result.getString("name"));
	                temp.setCourse(result.getInt("course"));	      
	                searchResults.add(temp);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (result != null)
	                    result.close();
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {}
	           
	         
	        }   
	        return searchResults;
	    } 


	public Subject select(Connection conn, String FOID) {
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        Subject s = null;
	        String sql = "SELECT * FROM Subjects WHERE (FOID = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, FOID);
	            result = stmt.executeQuery();

	            result.next();
	            s = new Subject();
	            s.setId(result.getInt("id"));
	            s.setName(result.getString("name"));	         
	            s.setCourse(result.getInt("course"));
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return s;
	}

	public String selectSubjectOID(Connection conn, String id) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        String oidp = null;
        String sql = "SELECT * FROM Subjects WHERE (FOID = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, id);
            result = stmt.executeQuery();

            result.next();
            oidp = result.getString("OID");
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return oidp;
	}

}
